package pl.enigmatic.jpf;

public class PropertyMetadata<X> implements CoerceValueCallback<X>, PropertyChangedCallback<X> {

	private final X defaultValue;
	private final CoerceValueCallback<X> coerce;
	private final PropertyChangedCallback<X> onChange;

	public PropertyMetadata(final X defaultValue, final CoerceValueCallback<X> coerce, final PropertyChangedCallback<X> onChange) {
		this.defaultValue = defaultValue;
		this.coerce = coerce;
		this.onChange = onChange;
	}

	public PropertyMetadata(final X defaultValue, final CoerceValueCallback<X> coerce) {
		this(defaultValue, coerce, null);
	}

	public PropertyMetadata(final X defaultValue, final PropertyChangedCallback<X> onChange) {
		this(defaultValue, null, onChange);
	}

	public PropertyMetadata(final X defaultValue) {
		this(defaultValue, null, null);
	}

	public PropertyMetadata() {
		this(null);
	}

	public X getDefaultValue() {
		return defaultValue;
	}

	public CoerceValueCallback<X> getCoerce() {
		return coerce;
	}

	public void onChange(final DependencyObject<?> obj, final DependencyPropertyChangedEventArgs<X> e) {
		if (onChange != null) {
			onChange.onChange(obj, e);
		}
	}

	public PropertyChangedCallback<X> getOnChange() {
		return onChange;
	}

	public X coerce(final DependencyObject<?> obj, final X value) {
		return coerce != null ? coerce.coerce(obj, value) : value;
	}
}
